<template>
    <div>
        <el-submenu :index="parentData.path" v-if="parentData.children">
            <template slot="title">
                <i :class="parentData.icon"></i>
                <span slot="title">{{ parentData.menuName }}</span>
            </template>
            <el-menu-item-group v-for="child in parentData.children" :key="child.component">
                <div v-if="child.hidden === 0"></div>
                <div v-else>
                    <template v-if="child.children  && child.children.length !=0">
                        <leftSubMenu :parent="child"></leftSubMenu>
                    </template>
                    <template v-else>
                        <el-menu-item @click="getItemChild(child)" :index="returnActive(child)">
                            <i :class="child.icon"></i>
                            <span>{{ child.menuName }}</span>
                        </el-menu-item>
                    </template>
                </div>
            </el-menu-item-group>
        </el-submenu>
        <el-menu-item @click="getItemOne(parentData)" v-else :index="returnActive(parentData)">
            <i :class="parentData.icon"></i>
            <span slot="title">{{ parentData.menuName }}</span>
        </el-menu-item>
    </div>
</template>

<script>
    export default {
        name: "leftSubMenu",
        props: {
            parent: Object,
        },
        data() {
            return {
                parentData: '',
            }
        },
        created() {
            this.parentData = this.parent
        },
        computed: {},
        methods: {
            returnActive(child) {
                let arr = this.getparentlist(child.path, this.$store.state.permission.topbarRouters)
                let arr2 = arr
                let str = ''
                for (let i = 0; i < arr2.length; i++) {
                    if (i !=0){
                        arr2[i] = '/'+arr2[i]
                    }
                    str+=arr2[i]
                }
                return str
            },
            getparentlist(path, tree) {
                let arr = [] //要返回的数组
                for (let i = 0; i < tree.length; i++) {
                    let item = tree[i]
                    arr = []
                    arr.push(item.path) //保存当前节点id
                    if (path == item.path) { //判断当前id是否是默认id
                        return arr //是则退出循环、返回数据
                    } else { //否则进入下面判断，判断当前节点是否有子节点数据
                        if (item.children && item.children.length > 0) {
                            //合并子节点返回的数据
                            arr = arr.concat(this.getparentlist(path, item.children))
                            if (arr.includes(path)) { //如果当前数据中已包含默认节点，则退出循环、返回数据
                                return arr
                            }
                        }
                    }
                }
            },

            getItemOne(parent) {
                console.log(parent)
                this.$router.push({path: parent.path})
            },
            getItemParent(item) {
                console.log(item)
            },

            getItemChild(child) {
                let arr = this.getparentlist(child.path, this.$store.state.permission.topbarRouters)
                let arr2 = arr
                let str = ''
                for (let i = 0; i < arr2.length; i++) {
                    if (i !=0){
                        arr2[i] = '/'+arr2[i]
                    }
                    str+=arr2[i]
                }
                this.$router.push({path: str})
            },
        }
    }
</script>

<style scoped>

</style>
